package com.itfenghuang.day01.day02;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public class A06_复习  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决设置post请求中文乱码问题
        req.setCharacterEncoding("utf-8");

        /**         一
         * 请求对象的常用获取路径的方法
         */
        // 1).获取请求对象虚拟目录的名称
        String contextPath = req.getContextPath();
        System.out.println("素胚勾勒出青花"+contextPath);

        // 2).获取请求对象Servlet映射路径
        String servletPath = req.getServletPath();
        System.out.println("笔峰浓转淡"+servletPath);

        // 3).获取请求对象访问者IP地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println("瓶身描绘的牡丹"+remoteAddr);

        // 4).获取请求对象的消息数据
        String queryString = req.getQueryString();
        System.out.println("一如你初妆"+queryString);

        // 5).获取统一资源定位符
        //协议+主机名+端口号+虚拟目录+访问资源路径
        //http://localhost:8080/request/tianServlet
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("冉冉檀香透过窗"+requestURL);

        // 6).获取统一资源标识符
        String requestURI = req.getRequestURI();
        System.out.println("心事我了然"+requestURI);

        /**          二
         * 请求对象常用方法之获取请求头信息的方法
         */

        //根据请求头的名称获取请求头的值
        String connection = req.getHeader("Connection");
        System.out.println("宣纸上走笔至此搁一半"+connection);
        String Length = req.getHeader("Content-Length");
        System.out.println("釉色渲染仕女图"+Length);
        String accept = req.getHeader("Accept-Encoding");
        System.out.println("韵味被私藏"+accept);

        /**
         *  请求对象常用方法之获取请求参数信息的方法
         */
        // 1).根据名称获取请求参数值
//        String username = req.getParameter("username");
//        System.out.println("而你嫣然的一笑"+username);
//        String password = req.getParameter("password");
//        System.out.println("如含苞待放"+password);

        // 2).根据名称获取所有数据
//        String[] hobby = req.getParameterValues("hobby");
//        System.out.println("你的美一缕飘散"+Arrays.toString(hobby));

        // 3).获取所有请求参数名称
        Enumeration<String> names = req.getParameterNames();
        while (names.hasMoreElements()){
            String name = names.nextElement();
            //req.getParameter()
            String[] para = req.getParameterValues(name);
            System.out.println(name+"="+Arrays.toString(para));
        }


        // 4).获取所有的参数的一个键值对集合
        Map<String, String[]> map = req.getParameterMap();
        Set<Map.Entry<String, String[]>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, String[]>> it = entrySet.iterator();
        while (it.hasNext()){
            Map.Entry<String, String[]> next = it.next();
            String key = next.getKey();
            String[] value = next.getValue();
            System.out.println(key+" "+Arrays.toString(value));
        }


        //解决中文响应乱码问题
        resp.setContentType("text/html;charset=utf-8");

        // 1).使用字符流响应数据
        PrintWriter writer = resp.getWriter();
        writer.write("<a href='https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=cos%E5%9B%BE%E7%89%87%20%E4%BA%BA%E7%89%A9&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=undefined&latest=undefined&copyright=undefined&cs=1488771794,3470206853&os=2357066423,3211562410&simid=4179807450,697073348&pn=1&rn=1&di=85580&ln=1438&fr=&fmq=1389861203899_R&fm=&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&oriquery=cos%E5%9B%BE%E7%89%87&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fi1.hdslb.com%252Fbfs%252Farchive%252F38f48d250297b112566e31cc6d85a1bf98167d26.jpg%26refer%3Dhttp%253A%252F%252Fi1.hdslb.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1643979717%26t%3D85e35204da6b668740f532f2130bac9c&rpstart=0&rpnum=0&adpicid=0&nojc=undefined&dyTabStr=MCwzLDIsNCwxLDYsNSw3LDgsOQ%3D%3D'>你有很多饭吃不下???</a>");
        writer.write("<h1>做人要鬼屎一点</h1>");

        // 2).通过字节流响应浏览器
//        ServletOutputStream os = resp.getOutputStream();
//        os.write("<h1>抱抱<h1>".getBytes());
//        OutputStreamWriter osw = new OutputStreamWriter(os);
//        osw.write("起飞咯");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    }

}
